package com.tanhua.server.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.dubbo.server.api.VideoApi;
import com.tanhua.dubbo.server.pojo.Video;
import com.tanhua.server.pojo.User;
import com.tanhua.server.utils.UserThreadLocal;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.MessagingException;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: Administrator
 * @date: Created in 2021/2/26 21:56
 * @version: 0.0.1
 * @modified By:
 */
@Service
public class VideoMQService {

    private static final Logger LOGGER=LoggerFactory.getLogger(VideoMQService.class);

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Reference(version = "1.0.0")
    private VideoApi videoApi;
    /**
     * @description 发布小视频消息
     * @author
     * @date
     * @param
     *@return
     */
    public void videoMsg(String videoId){
        this.sendMsg(1,videoId);
    }

    /**
     * @description 点赞小视频
     * @author
     * @date
     * @param
     *@return
     */
    public void likeVideoMsg(String videoId){
        this.sendMsg(2,videoId);
    }

    /**
     * @description 取消点赞小视频
     * @author
     * @date
     * @param
     *@return
     */
    public void disLikeVideoMsg(String videoId){
        this.sendMsg(3,videoId);
    }
    /**
     * @description 评论小视频
     * @author
     * @date
     * @param
     *@return
     */
    public void commentVideoMsg(String videoId){
        this.sendMsg(4,videoId);
    }

    /**
     * @description 发送小视频相关操作消息
     * @author
     * @date
     * @param
     *@return
     */
    private void sendMsg(Integer type,String videoId){
        try {
            User user= UserThreadLocal.get();
            Video video= this.videoApi.queryVideoById(videoId);
            //构建消息
            Map<String,Object>msg=new HashMap<>();
            msg.put("type",type);
            msg.put("userId",user.getId());
            msg.put("vid",video.getVid());
            msg.put("date",System.currentTimeMillis());
            msg.put("videoId",videoId);
            this.rocketMQTemplate.convertAndSend("tanhua-video",msg);
        } catch (MessagingException e) {
            LOGGER.error("发送视频失败！type=" + type + ",videoId=" + videoId, e);
        }

    }

}
